package synchThread;

/**
 * @author 李泽伟
 */
public class Main {
    public static void main(String[] args) {
        Thread t1 = new ThThread();
        Thread t2 = new ThThread();
        Thread t3 = new ThThread();

        t1.setName("线程1");
        t2.setName("线程2");
        t3.setName("线程3");

        t1.start();
        t2.start();
        t3.start();
    }
}

class ThThread extends Thread {
    private static int ticket = 100;
    @Override
    public void run() {
        while (ticket > 0) {
            show();
        }
    }
    /**
     * 额外造一个方法,称为同步方法，同步监视器：this
     */
    private static synchronized void show(){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(Thread.currentThread().getName() + ":" + ticket);
        ticket--;
    }
}